package com.by.wx.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.by.common.conf.TelConfMager;
import com.by.common.cont.TelConst;
import com.by.common.util.HttpUtil;
import com.by.common.util.SmsUtils;
import com.by.common.util.ThreadLocalDateUtil;
import com.by.wx.dao.PhoneSmsDao;
import com.by.wx.entity.PhoneCode;
import com.by.wx.service.PhoneSmsService;

@Service
public class PhoneSmsServiceImpl implements PhoneSmsService {

	public static Log log = LogFactory.getLog(PhoneSmsServiceImpl.class);

	@Autowired
	private PhoneSmsDao phoneSmsDao;
	
	public String CreateDateToJumpPermission(String phone,int lim) {
		List<PhoneCode> entity = phoneSmsDao.findCreatePhoneSmsByMimutes(phone,lim);
		if (entity.size() == 0) {
			return null;
		}
		try {
			Date date = new Date();
			long compNow = date.getTime();
//			String now = ThreadLocalDateUtil.formatDate(date);
//			String nowy = now.split(" ")[0].split("-")[0];
//			String nowm = now.split(" ")[0].split("-")[1];
//			String nowd = now.split(" ")[0].split("-")[2];
//			String yesterd =String.valueOf(Integer.parseInt(nowd)-1).length()==1? "0"+String.valueOf(Integer.parseInt(nowd)-1) :String.valueOf(Integer.parseInt(nowd)-1);
//			String tomorrd =String.valueOf(Integer.parseInt(nowd)+1).length()==1? "0"+String.valueOf(Integer.parseInt(nowd)+1) :String.valueOf(Integer.parseInt(nowd)+1);
//			String flagTime = nowy+"-"+nowm+"-"+tomorrd+" 12:00:00";
			
			String yest =ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[0]+"-"+ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[1]+"-"
				+(String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])-1).length()==1? "0"+String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])-1):String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])-1))
				+" "+TelConfMager.getProperty(TelConst.HOTOL_EXPIRATION_TIME_LIMITED_HOURS_MINUTES_SECONDS);
			
			String tomor =ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[0]+"-"+ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[1]+"-"
					+(String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])+1).length()==1? "0"+String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])+1):String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])+1))
					+" "+TelConfMager.getProperty(TelConst.HOTOL_EXPIRATION_TIME_LIMITED_HOURS_MINUTES_SECONDS);
			
			long yesterFlagTime = ThreadLocalDateUtil.parse(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[0]+"-"+ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[1]+"-"
					+(String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])-1).length()==1? "0"+String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])-1):String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])-1))
					+" "+TelConfMager.getProperty(TelConst.HOTOL_EXPIRATION_TIME_LIMITED_HOURS_MINUTES_SECONDS)).getTime();
			
			long nowFlagTime = ThreadLocalDateUtil.parse(ThreadLocalDateUtil.formatDate(date).split(" ")[0]+" 12:00:00").getTime();
			
			long tomoFlagTime = ThreadLocalDateUtil.parse(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[0]+"-"+ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[1]+"-"
					+(String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])+1).length()==1? "0"+String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])+1):String.valueOf(Integer.parseInt(ThreadLocalDateUtil.formatDate(date).split(" ")[0].split("-")[2])+1))
					+" "+TelConfMager.getProperty(TelConst.HOTOL_EXPIRATION_TIME_LIMITED_HOURS_MINUTES_SECONDS)).getTime();
			
			// 可能以后的业务需要对入主控制房间设备后的短信进行时效认证，判断是否超过某个时间点
			if (yesterFlagTime<compNow && compNow <= nowFlagTime) {
//				System.out.println("today am");
			}else if (compNow>nowFlagTime && nowFlagTime<=tomoFlagTime) {
//				System.out.println("today pm");
			}else {
//				System.out.println("error");
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
		}
//		if (entity.get(0).getGenerateTime()) {
//			
//		}
		return null;
	}

	@Override
	public String findCreatePhoneSmsByMimutes(String phone, int lim) {
		List<PhoneCode> entity = phoneSmsDao.findCreatePhoneSmsByMimutes(phone, lim);
		String rod = null;
		if (entity.size() == 0) {
			rod = HttpUtil.smsCode();
			return rod;
		}
		try {
			Date date = new Date();
			long compNow = date.getTime();
			long hisTime = ThreadLocalDateUtil.parse(ThreadLocalDateUtil.formatDate(entity.get(0).getGenerateTime())).getTime();
			long minuteTime = Long.valueOf(TelConfMager.getProperty(TelConst.MIAODIYUN_COM_PHONE_FAILURE_TIME_MILLIS)) / 10000 * 60 * 1000L;
			if (compNow > 0 && compNow <= (hisTime + minuteTime)) {
				rod = entity.get(0).getCode();
				return rod;
			} else {
				rod = HttpUtil.smsCode();
				return rod;
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String findCreateAliyunPhoneSmsByMimutes(String phone) {
		List<PhoneCode> entity = phoneSmsDao.dofindAliyunSmsByphone(phone);
		Date dt = new Date();
		try {
			String sdt = ThreadLocalDateUtil.formatDate(dt);
			String flagDate = sdt.split(" ")[0];
			for (PhoneCode phoneCode : entity) {
				if (String.valueOf(phoneCode.getGenerateTime()).contains(flagDate)) {
					// System.out.println("a="+phoneCode.getGenerateTime());
				}
				// System.out.println(String.valueOf(phoneCode.getGenerateTime()));
			}
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		String rod = null;
		if (entity.size() == 0) {
			rod = SmsUtils.generateAliyunMsgCode();
			// System.out.println("Aliyun no generatetime");
			return rod;
		}
		try {
			Date date = new Date();
			long compNow = date.getTime();
			long hisTime = ThreadLocalDateUtil.parse(ThreadLocalDateUtil.formatDate(entity.get(0).getGenerateTime())).getTime();
			// System.out.println(ThreadLocalDateUtil.formatDate(new
			// Date(hisTime)));
			long minuteTime = Long.valueOf(TelConfMager.getProperty(TelConst.ALIYUN_COM_PHONE_SMS_VALID_PAST_SECONDS)) * 1000L;
			if (compNow > 0 && compNow <= (hisTime + minuteTime)) {
				rod = entity.get(0).getCode();
			} else {
				rod = SmsUtils.generateAliyunMsgCode();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return rod;
	}

	@Override
	public List<PhoneCode> doFindAliyunSmsCodeByPhone(String phone) {
		if (StringUtils.isEmpty(phone))
			throw new RuntimeException("手机号码不能为空");
		return phoneSmsDao.findCreatePhoneSmsByMimutes(phone, 1);
	}

	@Override
	public List<PhoneCode> doFindCheckVerifyCodeByOpenid(String openid) {
		return phoneSmsDao.doFindCheckVerifyCodeByOpenid(openid);
	}

	@Override
	public List<PhoneCode> findHistoryPhoneSmsByOpenid(String openid) {
		return phoneSmsDao.findHistoryPhoneSmsByOpenid(openid, 1);
	}

//	@Override
//	public List<PhoneCode> doFindCodeByPhone(String phone, int lim) {
//		if (StringUtils.isEmpty(phone)) 
//		throw new RuntimeException("手机号码不能为空");
//		return phoneSmsDao.CreatePhoneSmsByMimutes(phone,lim);
//	}
	
}
